package com.bjtct.plugin.autogen.wizards;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ContainerSelectionDialog;

public class GenCodePathPage extends WizardPage {

	private ISelection selection;
	private Text actionPath;
	private Text viewPath;
	private Text javaName;
	private Combo combo;
	private boolean[] gen = new boolean[] { true, true, true, true, true, true, true, true };
	private Text servicePath;
	private Text daoPath;
	private Text domainPath;

	/**
	 * Create the wizard.
	 */
	public GenCodePathPage() {
		super("wizardPage");
		setTitle("Wizard Page title");
		setDescription("Wizard Page description");
	}

	/**
	 * @wbp.parser.constructor
	 */
	public GenCodePathPage(ISelection selection) {
		super("gcpppage");
		this.selection = selection;
		setTitle("路径设置");
		setDescription("设置相关文件生成以及存放路径!");
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);

		Group group = new Group(container, SWT.NONE);
		group.setText("生成文件");
		group.setBounds(10, 10, 564, 78);

		final Button btnCheckButton = new Button(group, SWT.CHECK);
		btnCheckButton.setSelection(true);
		btnCheckButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[0] = btnCheckButton.getSelection();
				dialogChage();
			}
		});
		btnCheckButton.setBounds(10, 24, 98, 17);
		btnCheckButton.setText("控制类");

		final Button btnCheckButton_1 = new Button(group, SWT.CHECK);
		btnCheckButton_1.setSelection(true);
		btnCheckButton_1.setBounds(114, 24, 98, 17);
		btnCheckButton_1.setText("业务接口");
		btnCheckButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[1] = btnCheckButton_1.getSelection();
				dialogChage();
			}
		});

		final Button btnCheckButton_2 = new Button(group, SWT.CHECK);
		btnCheckButton_2.setSelection(true);
		btnCheckButton_2.setBounds(220, 24, 98, 17);
		btnCheckButton_2.setText("业务实现");
		btnCheckButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[2] = btnCheckButton_2.getSelection();
				dialogChage();
			}
		});

		final Button btnCheckButton_3 = new Button(group, SWT.CHECK);
		btnCheckButton_3.setSelection(true);
		btnCheckButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[3] = btnCheckButton_3.getSelection();
				dialogChage();
			}
		});
		btnCheckButton_3.setBounds(324, 24, 98, 17);
		btnCheckButton_3.setText("数据接口");

		final Button btnCheckButton_4 = new Button(group, SWT.CHECK);
		btnCheckButton_4.setSelection(true);
		btnCheckButton_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[4] = btnCheckButton_4.getSelection();
				dialogChage();
			}
		});
		btnCheckButton_4.setBounds(10, 47, 98, 17);
		btnCheckButton_4.setText("数据实现");

		final Button btnCheckButton_5 = new Button(group, SWT.CHECK);
		btnCheckButton_5.setSelection(true);
		btnCheckButton_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[5] = btnCheckButton_5.getSelection();
				dialogChage();
			}
		});
		btnCheckButton_5.setBounds(114, 47, 98, 17);
		btnCheckButton_5.setText("实体类");

		final Button btnCheckButton_6 = new Button(group, SWT.CHECK);
		btnCheckButton_6.setSelection(true);
		btnCheckButton_6.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[6] = btnCheckButton_6.getSelection();
				dialogChage();
			}
		});
		btnCheckButton_6.setBounds(220, 47, 98, 17);
		btnCheckButton_6.setText("查询类");

		final Button btnCheckButton_7 = new Button(group, SWT.CHECK);
		btnCheckButton_7.setSelection(true);
		btnCheckButton_7.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				gen[7] = btnCheckButton_7.getSelection();
				dialogChage();
			}
		});
		btnCheckButton_7.setBounds(324, 47, 98, 17);
		btnCheckButton_7.setText("页面");

		Group group_1 = new Group(container, SWT.NONE);
		group_1.setText("路径设置");
		group_1.setBounds(10, 94, 564, 218);

		Label lblNewLabel = new Label(group_1, SWT.NONE);
		lblNewLabel.setBounds(10, 25, 52, 25);
		lblNewLabel.setText("控制类：");

		Label lblNewLabel_1 = new Label(group_1, SWT.NONE);
		lblNewLabel_1.setBounds(10, 55, 55, 25);
		lblNewLabel_1.setText("页   面：");

		actionPath = new Text(group_1, SWT.BORDER);
		actionPath.setBounds(68, 26, 115, 23);
		viewPath = new Text(group_1, SWT.BORDER);
		viewPath.setBounds(68, 57, 115, 23);
		servicePath = new Text(group_1, SWT.BORDER);
		servicePath.setBounds(68, 86, 115, 23);
		daoPath = new Text(group_1, SWT.BORDER);
		daoPath.setBounds(68, 115, 115, 23);
		domainPath = new Text(group_1, SWT.BORDER);
		domainPath.setBounds(68, 145, 115, 23);

		Button btAction = new Button(group_1, SWT.NONE);
		btAction.setBounds(189, 24, 52, 27);
		btAction.setText("浏览...");
		Button btView = new Button(group_1, SWT.NONE);
		btView.setBounds(189, 55, 52, 27);
		btView.setText("浏览...");

		Label label = new Label(group_1, SWT.NONE);
		label.setAlignment(SWT.CENTER);
		label.setBounds(265, 25, 55, 25);
		label.setText("文件名：");

		javaName = new Text(group_1, SWT.BORDER);
		javaName.setBounds(326, 22, 110, 23);
		String java = loadData("selectTable");
		java = java == null ? "" : java.replaceFirst("t_", "");
		javaName.setText(java);

		Label label_1 = new Label(group_1, SWT.NONE);
		label_1.setBounds(265, 55, 55, 25);
		label_1.setText("展示风格：");
		combo = new Combo(group_1, SWT.NONE);
		combo.setItems(new String[] { "TABLE", "TREE", "TREE_TABLE" });
		combo.setBounds(326, 55, 110, 25);
		combo.select(0);

		Label lblNewLabel_2 = new Label(group_1, SWT.NONE);
		lblNewLabel_2.setBounds(10, 85, 55, 25);
		lblNewLabel_2.setText("业务类：");
		Button btService = new Button(group_1, SWT.NONE);
		btService.setText("浏览...");
		btService.setBounds(189, 86, 52, 27);
		Label label_2 = new Label(group_1, SWT.NONE);
		label_2.setBounds(10, 115, 55, 25);
		label_2.setText("数据类：");
		Label lblNewLabel_3 = new Label(group_1, SWT.NONE);
		lblNewLabel_3.setBounds(10, 145, 55, 25);
		lblNewLabel_3.setText("实体类：");
		Button btDao = new Button(group_1, SWT.NONE);
		btDao.setText("浏览...");
		btDao.setBounds(189, 115, 52, 27);
		Button btDomain = new Button(group_1, SWT.NONE);
		btDomain.setText("浏览...");
		btDomain.setBounds(189, 145, 52, 27);

		btAction.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
						ResourcesPlugin.getWorkspace().getRoot(), false, "选择控制类存放路径 !");
				if (dialog.open() == ContainerSelectionDialog.OK) {
					Object[] result = dialog.getResult();
					if (result.length == 1) {
						getActionPath().setText(((Path) result[0]).toString());
					}
				}
			}
		});
		btView.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
						ResourcesPlugin.getWorkspace().getRoot(), false, "选择视图文件存放 路径 !");
				if (dialog.open() == ContainerSelectionDialog.OK) {
					Object[] result = dialog.getResult();
					if (result.length == 1) {
						getViewPath().setText(((Path) result[0]).toString());
					}
				}
			}
		});
		btService.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
						ResourcesPlugin.getWorkspace().getRoot(), false, "选择业务类存放 路径 !");
				if (dialog.open() == ContainerSelectionDialog.OK) {
					Object[] result = dialog.getResult();
					if (result.length == 1) {
						getServicePath().setText(((Path) result[0]).toString());
					}
				}
			}
		});
		btDao.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
						ResourcesPlugin.getWorkspace().getRoot(), false, "选择视数据类存放 路径 !");
				if (dialog.open() == ContainerSelectionDialog.OK) {
					Object[] result = dialog.getResult();
					if (result.length == 1) {
						getDaoPath().setText(((Path) result[0]).toString());
					}
				}
			}
		});
		btDomain.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ContainerSelectionDialog dialog = new ContainerSelectionDialog(getShell(),
						ResourcesPlugin.getWorkspace().getRoot(), false, "选择视实体类存放 路径 !");
				if (dialog.open() == ContainerSelectionDialog.OK) {
					Object[] result = dialog.getResult();
					if (result.length == 1) {
						getDomainPath().setText(((Path) result[0]).toString());
					}
				}
			}
		});
		javaName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validFileName();
			}
		});
		actionPath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				dialogChage();
			}
		});
		viewPath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				dialogChage();
			}
		});
		servicePath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				dialogChage();
			}
		});
		daoPath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				dialogChage();
			}
		});
		domainPath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				dialogChage();
			}
		});
		validFileName();// 验证java文件名
		dialogChage();
		// dialogChage(actionPath.getText(), true, "设置控制类路径！");

		/*
		 * dialogChage(viewPath.getText(), true, "设置视图路径！");
		 * dialogChage(servicePath.getText(), true, "设置业务类路径！");
		 * dialogChage(daoPath.getText(), true, "设置数据类路径！");
		 * dialogChage(domainPath.getText(), true, "设置实体类路径!");
		 */

	}

	private void dialogChage() {
		if (gen[0]) {// 选择生成时才难
			String path = actionPath.getText();
			IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
			if (path.length() == 0) {
				updateStatus("设置控制类路径");
				return;
			}
			if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
				updateStatus(path + "不存在!");
				return;
			}
			if (!container.isAccessible()) {
				updateStatus(path + "不可写!");
				return;
			}
		}
		if (gen[7]) {// 选择生成时才难
			String path = viewPath.getText();
			IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
			if (path.length() == 0) {
				updateStatus("设置视图路径");
				return;
			}
			if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
				updateStatus(path + "不存在!");
				return;
			}
			if (!container.isAccessible()) {
				updateStatus(path + "不可写!");
				return;
			}
		}
		if (gen[1] || gen[2]) {// 选择生成时才难
			String path = servicePath.getText();
			IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
			if (path.length() == 0) {
				updateStatus("设置业务类路径");
				return;
			}
			if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
				updateStatus(path + "不存在!");
				return;
			}
			if (!container.isAccessible()) {
				updateStatus(path + "不可写!");
				return;
			}
		}
		if (gen[3] || gen[4]) {// 选择生成时才难
			String path = daoPath.getText();
			IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
			if (path.length() == 0) {
				updateStatus("设置数据类路径");
				return;
			}
			if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
				updateStatus(path + "不存在!");
				return;
			}
			if (!container.isAccessible()) {
				updateStatus(path + "不可写!");
				return;
			}
		}
		if (gen[5] || gen[6]) {// 选择生成时才难
			String path = domainPath.getText();
			IResource container = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
			if (path.length() == 0) {
				updateStatus("设置实体类路径");
				return;
			}
			if (container == null || (container.getType() & (IResource.PROJECT | IResource.FOLDER)) == 0) {
				updateStatus(path + "不存在!");
				return;
			}
			if (!container.isAccessible()) {
				updateStatus(path + "不可写!");
				return;
			}
		}
	
		updateStatus(null);
	}

	private void validFileName() {
		String fileName = javaName.getText();
		if (fileName.length() == 0) {
			updateStatus("必须是有效的文件名,只能是字母,不包含特殊字符!");
			return;
		}
		if (fileName.replace('\\', '/').indexOf('/') > 0 || fileName.indexOf('.') > 0) {
			updateStatus("必须是有效的文件名,只能是字母,不包含特殊字符!");
			return;
		}

		updateStatus(null);
	}

	private String loadData(String name) {
		IDialogSettings ds = getWizard().getDialogSettings();
		String value = null;
		if (ds != null) {
			value = ds.get(name);
		}
		return value;
	}

	public String getShowStyle() {
		int index = combo.getSelectionIndex();
		
		if (index == 0)
			return "TABLE";
		if (index == 1)
			return "TREE";
		if (index == 2)
			return "TREE_TABLE";
		return "TABLE";
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);

	}

	public ISelection getSelection() {
		return selection;
	}

	public void setSelection(ISelection selection) {
		this.selection = selection;
	}

	public Text getActionPath() {
		return actionPath;
	}

	public void setActionPath(Text actionPath) {
		this.actionPath = actionPath;
	}

	public Text getViewPath() {
		return viewPath;
	}

	public void setViewPath(Text viewPath) {
		this.viewPath = viewPath;
	}

	public Text getJavaName() {
		return javaName;
	}

	public void setJavaName(Text javaName) {
		this.javaName = javaName;
	}

	public Combo getCombo() {
		return combo;
	}

	public void setCombo(Combo combo) {
		this.combo = combo;
	}

	public boolean[] getGen() {
		return gen;
	}

	public void setGen(boolean[] gen) {
		this.gen = gen;
	}

	public Text getServicePath() {
		return servicePath;
	}

	public void setServicePath(Text servicePath) {
		this.servicePath = servicePath;
	}

	public Text getDaoPath() {
		return daoPath;
	}

	public void setDaoPath(Text daoPath) {
		this.daoPath = daoPath;
	}

	public Text getDomainPath() {
		return domainPath;
	}

	public void setDomainPath(Text domainPath) {
		this.domainPath = domainPath;
	}

}
